package com.adv.ems.model.vo;

import cn.hutool.core.date.DateUtil;
import com.adv.ems.db.entity.*;
import com.adv.ems.enums.CollectPointStateEnum;
import com.adv.ems.utils.EmsUtil;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
public class EnergySearchOnlineValueVO {
    private List<Info> list;

    public EnergySearchOnlineValueVO() {
        this.list = new ArrayList<Info>();
    }

    @Data
    public static class Info {
        private String meterCode;
        private String meterName;
        private String pointName;
        private String energyName;
        private String itemName;
        private String gateWayName;
        private Double lastUploadRealValue;
        private String lastUploadTime;
        private String state;
        private String stateDesc;

        public Info(MeterDO meter, CollectPointDO point, ItemInfoDO itemInfo, EnergyInfoDO energy, GatewayDeviceDO gateway, String acceptLanguage) {
            if (meter != null) {
                this.meterCode = meter.getCode();
                this.meterName = meter.getName();
            }
            this.pointName = point.getName();
            if (energy != null) {
                this.energyName = EmsUtil.multilingual(energy.getNameInfo(), acceptLanguage);
            }
            if (itemInfo != null) {
                this.itemName = EmsUtil.multilingual(itemInfo.getNameInfo(), acceptLanguage);
            }
            if (gateway != null) {
                this.gateWayName = gateway.getName();
            }
            this.lastUploadRealValue = point.getLastUploadRealValue();
            this.lastUploadTime = point.getLastUploadTime() == null ? null : DateUtil.formatDateTime(point.getLastUploadTime());
            if (CollectPointStateEnum.REFUSE.name().equals(point.getState())) {
                this.stateDesc = "已停止接收数据";
                this.state = "Y";
            }else {
                Map.Entry<Boolean, String> deviceState = EmsUtil.getDeviceState(point.getLastUploadTime());
                this.stateDesc = deviceState.getValue();
                this.state = deviceState.getKey() ? "Y": "N";
            }
        }
    }
}
